<?php

namespace Acme\TaskBundle\Controller;

use Symfony\Component\Validator\Constraints\Email;

use Symfony\Component\Validator\Constraints\MinLength;

use Symfony\Component\Validator\Constraints\Collection;

use Acme\TaskBundle\Repository\TaskRepository;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Validator\Constraints;

use Acme\TaskBundle\Entity\Task;
use Acme\TaskBundle\Form\Type\TaskType;
use Acme\TaskBundle\Repository;

class DefaultController extends Controller
{
  public function newAction(Request $request)
  {  	
  	$collectionConstraint = new Collection(array(
  			'name' => new MinLength(5),
  			'email' => new Email(array('message' => 'Invalid email!'))
  			));
  	
  	$form = $this->createFormBuilder(null, array(
  			'validation_constraint' => $collectionConstraint
  			))
  			->add('email', 'email')
  			->add('name')
  			->getForm();
  	
  	return $this->render('AcmeTaskBundle:Default:contact.html.twig',
  			array('form' => $form->createView())); 
  	/* $task = new Task();
    $task->setTask('Write a blog post');
    $task->setDueDate(new \DateTime('tomorrow'));
    $task->setDescription('Empty description');

    $form = $this->createForm(new TaskType(), $task);
//     $form = $this->createFormBuilder($task)
//                  ->add('task', 'text')
//                  ->add('dueDate', 'date', array('widget' => 'single_text'))
//                  ->getForm();
        
    if($request->getMethod() == 'POST')
    {
    	$form->bindRequest($request);
    	
    	if($form->isValid())
    	{
    		// get object task from form object
    		$task = $form->getData();
    		
    		// store information of request
//     		$repository = $this->getDoctrine()->getRepository('AcmeTaskBundle:Task');    		    	
//     		$repository->storeTask($task);
    		$em = $this->getDoctrine()->getEntityManager();
    		$em->persist($task);
    		$em->flush();
    		
    		return $this->redirect($this->generateUrl('task_success'));		
    	}
    }
    
    
    return $this->render('AcmeTaskBundle:Default:new.html.twig', 
    					 array('form' => $form->createView())
    		             ); */     
  }  

  
  public function successAction()
  {
  	return new Response('success!');
  }
  
  public function contactAction(Request $request)
  {
    $defaultData = array('message' => 'Type your message here.');

    $form = $this->createFormBuilder($defaultData)
                 ->add('name', 'text')
                 ->add('email', 'email')
                 ->add('message', 'textarea')
                 ->getForm();
    if($request->getMethod() == 'POST')
    {
    	$form->bindRequest($request);
    	
    	// data is an array with name, email, message keys
    	$data = $form->getData();
    }
    
    // render the form
  }
  
  public function indexAction($name)
  {
    	// create task and give it some dummy data
    	$task = new Task();
    	$task->setTask('Write a blog post');
    	$task->setDueDate(new \DateTime('tomorrow'));
    	
    	$form = $this->createFormBuilder($task)
    	             ->add('task', 'text')
    	             ->add('dueDate', 'date')
    	             ->getForm();
    	
      return $this->render('AcmeTaskBundle:Default:index.html.twig', array('form' => $form->createView()));
  }
}
